﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using FoundationClass.Entity;

namespace FoundationClass.Data
{
    public static class ComputerData
    {
        public static void InsertComputer(Computer computer)
        {
            try
            {
                DataTable computerDt = DatabaseManager.Read("Computer");
                int newId = GetMaxComputerId() + 1;
                computerDt.Rows.Add(newId, computer.ComputerName , computer.Specification, computer.Status, computer.ComputerType);
                DatabaseManager.Write(computerDt);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private static int GetMaxComputerId()
        {
            DataTable computerDt = DatabaseManager.Read("Computer");
            int rowcount = computerDt.Rows.Count;
            if (rowcount == 0) 
                return 1;
            else 
                return int.Parse(computerDt.Rows[rowcount - 1]["id"].ToString());
        }

        public static void UpdateComputer(Computer computer)
        {
            DataTable computerDt = DatabaseManager.Read("Computer");
            foreach (DataRow row in computerDt.Rows)
            {
                if (row["id"].Equals(computer.Id.ToString()))
                {
                    row["cname"] = computer.ComputerName;
                    row["specification"] = computer.Specification;
                    row["status"] = computer.Status;
                    row["type"] = computer.ComputerType;
                    break;
                }
            }
            DatabaseManager.Write(computerDt);
        }

        public static void DeleteComputer(int id)
        {
            DataTable computerDt = DatabaseManager.Read("Computer");
            foreach (DataRow row in computerDt.Rows)
            {
                if (row["id"].Equals(id.ToString()))
                {
                    row.Delete();
                    break;
                }
            }
            DatabaseManager.Write(computerDt);
        }

        public static Computer GetComputer(int id)
        {
            try
            {
                Computer computer = null;
                DataTable computerDt = DatabaseManager.Read("Computer");
                foreach (DataRow row in computerDt.Rows)
                {
                    if ((row["id"]).Equals(id.ToString()))
                    {
                        computer = new Computer
                        {
                            Id = Int32.Parse((row["id"].ToString())),
                            ComputerName = (string)row["cname"],
                            Specification = (string)row["specification"],
                            ComputerType = char.Parse(row["type"].ToString()),
                            Status = char.Parse(row["status"].ToString())
                        };
                        break;
                    }
                }
                return computer;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        public static IList<Computer> GetAllComputers()
        {
            IList<Computer> computers = null;
            DataTable computerDt = DatabaseManager.Read("Computer");
            if (computerDt.Rows.Count > 0)
            {
                computers = new List<Computer>();
                foreach (DataRow row in computerDt.Rows)
                {
                    computers.Add(new Computer()
                    {
                        Id = Int32.Parse(row["id"].ToString()),
                        ComputerName = (string)row["cname"],
                        Specification = (string)row["specification"],
                        Status = Char.Parse(row["status"].ToString()),
                        ComputerType = Char.Parse(row["type"].ToString())
                    });
                }
            }
            return computers;
        }
        public static IList<Computer> GetAllComputersActive()
        {
            IList<Computer> computers = null;
            DataTable computerDt = DatabaseManager.Read("Computer");
            if (computerDt.Rows.Count > 0)
            {
                computers = new List<Computer>();
                foreach (DataRow row in computerDt.Rows)
                {
                    if (Char.Parse(row["status"].ToString()) == 'A')
                    {
                        computers.Add(new Computer()
                                          {
                                              Id = Int32.Parse(row["id"].ToString()),
                                              ComputerName = (string) row["cname"],
                                              Specification = (string) row["specification"],
                                              Status = Char.Parse(row["status"].ToString()),
                                              ComputerType = Char.Parse(row["type"].ToString())
                                          });
                    }
                }
            
            }
            return computers;
        }

        public static void UpdateStatusComputer(int id ,char status)
        {
            DataTable computerDt = DatabaseManager.Read("Computer");
            foreach (DataRow row in computerDt.Rows)
            {
                if (row["id"].Equals(id.ToString()))
                {
                    row["status"] = status;
                    
                    break;
                }
            }
            DatabaseManager.Write(computerDt);
        }
    }
    
}
